﻿Imports System.IO

Public Class FormMain

    Public b1 As Bitmap = My.Resources.bat
    Public b2 As Bitmap = My.Resources.man
    Public b3 As Bitmap = My.Resources.batman
    Public c As Cypher = Nothing
    Public WithEvents f As New FormDitherPicker()
    Public currentImage As Image = Nothing

    Public imageSizePercent As Decimal = 100D
    Public imageSizePercentWidth As Decimal = 0
    Public imageSizePercentHeight As Decimal = 0

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
        Dim ditherSettings As New SelectedSettings(New AForge.Imaging.Filters.Threshold(), "Threshold", 0.33, 0.33, 0.33)
        c = New Cypher(b1, b2, b3, ditherSettings, ditherSettings, ditherSettings)
        p1.AllowDrop = True
        p2.AllowDrop = True
        p3.AllowDrop = True
        refreshesImageDisplay()
        refreshScaleDisplay()
        SplashModule.splashGraphics.Hide()
        SplashModule.splashText.Hide()
        BringToFront()
    End Sub

    Public Sub refreshesImageDisplay()
        p1.Image = Nothing
        p1.Image = c.image1.displayBitmapThumb
        p2.Image = Nothing
        p2.Image = c.image2.displayBitmapThumb
        p3.Image = Nothing
        p3.Image = c.image3.displayBitmapThumb
        d1.Image = Nothing
        d1.Image = c.image1.ditheredBitmapThumbForDisplay
        d2.Image = Nothing
        d2.Image = c.image2.ditheredBitmapThumbForDisplay
        d3.Image = Nothing
        d3.Image = c.image3.ditheredBitmapThumbForDisplay
        e1.Image = Nothing
        e1.Image = c.encoded1Thumb.bitmap
        e2.Image = Nothing
        e2.Image = c.encoded2Thumb.bitmap

        Dim centeredBackgroundImage As New Bitmap(250, 200)
        Using g = Graphics.FromImage(centeredBackgroundImage)
            'Paint the canvas white
            g.Clear(Color.White)
            Dim widthOffset As Integer = (250 - c.encoded1Thumb.bitmap.Width) \ 2
            Dim heightOffset As Integer = (200 - c.encoded1Thumb.bitmap.Height) \ 2
            g.DrawImageUnscaled(c.encoded1Thumb.bitmap, widthOffset, heightOffset)
        End Using

        Dim centeredForegroundImage As New Bitmap(250, 200)
        Using g = Graphics.FromImage(centeredForegroundImage)
            'Paint the canvas white
            g.Clear(Color.White)
            Dim widthOffset As Integer = (250 - c.encoded2Thumb.bitmap.Width) \ 2
            Dim heightOffset As Integer = (200 - c.encoded2Thumb.bitmap.Height) \ 2
            g.DrawImageUnscaled(c.encoded2Thumb.bitmap, widthOffset, heightOffset)
            centeredForegroundImage.MakeTransparent(Color.White)
        End Using

        o1.BackgroundImage = Nothing
        o1.BackgroundImage = centeredBackgroundImage


        o1.Image = Nothing
        o1.Image = centeredForegroundImage
    End Sub

    Private Sub buttonMakeFullsize_Click(sender As Object, e As EventArgs) Handles buttonMakeFullsize.Click
        Dim timeStamp As String = Now.ToString(" yyyy-MM-dd HH mm ss")
        Cursor = Cursors.WaitCursor
        labelSize.Text = "MAKING..."
        Application.DoEvents()
        c.image1.setSize(imageSizePercentWidth, imageSizePercentHeight)
        c.image2.setSize(imageSizePercentWidth, imageSizePercentHeight)
        c.image3.setSize(imageSizePercentWidth, imageSizePercentHeight)
        c.makeFullSize()
        'trackbarImageSizePercent.Value = 100
        'c.encoded1.bitmap.Save("encoded1.gif", Imaging.ImageFormat.Gif)
        'c.encoded2.bitmap.Save("encoded2.gif", Imaging.ImageFormat.Gif)
        'save both encoded images.
        If Not Directory.Exists("output") Then Directory.CreateDirectory("output")
        c.encoded1.bitmap.Save("output\Encoded 1 " & timeStamp & ".png", Imaging.ImageFormat.Png)
        c.encoded2.bitmap.Save("output\Encoded 2 " & timeStamp & ".png", Imaging.ImageFormat.Png)
        refreshScaleDisplay()
        Cursor = Cursors.Default
    End Sub

    Private Sub user_dragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles p1.DragEnter, p2.DragEnter, p3.DragEnter
        If e.Data.GetDataPresent(DataFormats.FileDrop) Then
            e.Effect = DragDropEffects.Copy
        End If
    End Sub

    Private Sub user_dragAndDrop(sender As Object, e As DragEventArgs) Handles p1.DragDrop, p2.DragDrop, p3.DragDrop
        Dim filename As String = CType(e.Data.GetData(DataFormats.FileDrop)(0), String)
        Dim senderInstance As PictureBox = CType(sender, PictureBox)
        Dim senderInstanceName As String = senderInstance.Name
        If ImageFileTypes.getFileType(filename) <> ImageFileTypes.ImageTypes.Unknown Then
            Debug.Print("Loading image...")
            '    Dim image As Image = image.FromFile(filename) ' this doesn't close the file!
            Dim image As Image = image.FromStream(New MemoryStream(File.ReadAllBytes(filename)))
            If senderInstanceName = "p1" Then c.setImage1(image)
            If senderInstanceName = "p2" Then c.setImage2(image)
            If senderInstanceName = "p3" Then c.setImage3(image)
        End If
        trackbarImageSizePercent.Value = 100
        refreshesImageDisplay()
        refreshScaleDisplay()
    End Sub

    Private Sub p1_Click(sender As Object, e As EventArgs) Handles d1.Click
        f.setImageToChangeDither(c.image1)
        f.ShowDialog()
    End Sub

    Private Sub p2_Click(sender As Object, e As EventArgs) Handles d2.Click
        f.setImageToChangeDither(c.image2)
        f.ShowDialog()
    End Sub

    Private Sub p3_Click(sender As Object, e As EventArgs) Handles d3.Click
        f.setImageToChangeDither(c.image3)
        f.ShowDialog()
    End Sub

    Private Sub pickedFilter(ByVal sender As Object, ByVal args As EventArgs) Handles f.filterSelected
        c.makeSmallSize()
        refreshesImageDisplay()
    End Sub

    Private Sub trackbarImageSizePercent_Scroll(sender As Object, e As EventArgs) Handles trackbarImageSizePercent.Scroll
        refreshScaleDisplay()
    End Sub

    Public Sub refreshScaleDisplay()
        imageSizePercent = trackbarImageSizePercent.Value / 2
        imageSizePercentWidth = (c.image1.originalBitmap.Width / 100D) * imageSizePercent
        imageSizePercentHeight = (c.image1.originalBitmap.Height / 100D) * imageSizePercent
        labelSize.Text = CInt(imageSizePercentWidth * 2) & " X " & CInt(imageSizePercentHeight * 2)
    End Sub
End Class
